package com.zhou.test.spring.secrity.security.athentication;

import com.zhou.common.lib.model.Response;
import com.zhou.common.lib.util.JSONUtils;
import com.zhou.test.spring.secrity.util.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Security 未登录处理 Handler
 * <p>
 * @serial 2.0.0
 * @author 就眠儀式
 */
@Slf4j
@Component
public class SecureAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        log.info("未登录不能访问");
        ServletUtil.write(JSONUtils.toJson(Response.error(401, "未知账户")));
    }
}
